home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / archie38_1.lha / archie-1.4 / archie.c < prev    next >
C/C++ Source or Header  |  1995-01-05  |  8KB  |  333 lines

  1. /*
  2.  * Copyright (c) 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <copyright.h>.
  6.  */
  7.  
  8. /*
  9.  * Archie client using the Prospero protocol.
  10.  *
  11.  * Suggestions and improvements to Brendan Kehoe (brendan@cygnus.com).
  12.  */
  13. /* Amiga port by Tomas Willis (tomas@cae.wisc.edu) January 1995 */
  14.  
  15. #include <stdio.h>
  16. #include "getopt.h"
  17.  
  18. #if defined(OS2)
  19. # include <pctcp.h>
  20. #endif
  21.  
  22. #ifdef MSDOS
  23. # include <string.h>
  24. # include <stdlib.h>
  25. # ifdef CUTCP
  26. #  include <msdos/cutcp.h>
  27. #  include <msdos/hostform.h>
  28. /* The default stack size for a BC program is 4k; jack it up to 16 and add the
  29.    Check for Stack Overflow option to the compiler.  */
  30. extern unsigned _stklen = 16 * 1024;
  31. # endif
  32. #endif
  33.  
  34. #ifdef AMIGA
  35. # include <string.h>
  36. # include <stdlib.h>
  37. #endif
  38.  
  39. #include "pfs.h"
  40. #include "rdgram.h"
  41. #include "archie.h"
  42. #include "pmachine.h"
  43.  
  44. /* Whether we should produce single-line listings suitable for frobbing by
  45.    other programs, or produce nice clean human output (default).  */
  46. int listflag = 0;
  47.  
  48. /* How to sort the data; 1 means by date, 0 is by inverse hostname.  */
  49. int sortflag = 0;
  50.  
  51. /* Used by CUTCP to see if they specified the host with `-h' or if
  52.    the config.tel file should be consulted.  */
  53. int hostset = 0;
  54.  
  55. /* When doing searches, should we make some comments to pacify the user?  */
  56. int verbose = 0;
  57.  
  58. /* Maximum number of hits for this query; pushing this high is very
  59.    anti-social.  */
  60. int max_hits = MAX_HITS;
  61.  
  62. /* The offset for the Prospero query.  */
  63. int offset = 0;
  64.  
  65. /* Display the Alex filename?  */
  66. int alex = 0;
  67.  
  68. /* The default host to query for searches.  */
  69. char *host = ARCHIE_HOST;
  70.  
  71. FILE *archie_out;
  72.  
  73. #ifdef AMIGA /* Requires Amiga OS 2.1, AmiTCP 2.3 or above, I suppose */
  74. # define VSTRING "Archie client 38.1 (5.1.95) [Amiga port by Tomas Willis]"
  75.   static const char *vers_tag    = "\0$VER: "VSTRING;
  76. #endif /* Amiga */
  77.  
  78. /* The name this program was run with.  */
  79. char *program_name;
  80.  
  81. extern int pfs_debug;
  82. extern int rdgram_priority;
  83.  
  84. void main (int argc, char **argv);
  85. void usage (void);
  86. //extern char *getenv (); //stdlib.h
  87. extern void procquery (char *host, char *str, int max_hits, int offset, Query query);
  88.  
  89.  
  90. void
  91. main (int argc, char **argv)
  92. {
  93.   Query query = EXACT;
  94.   int optc, tmp;
  95.   /* If true, display the release.  */
  96.   int exitflag = 0;
  97.   /* The file to print the results to.  Defaults to stdout.  */
  98.   char *outfile = (char *)NULL;
  99.   char *p;
  100.   static char *archies[] = { ARCHIES };
  101.  
  102. #ifdef AMIGA
  103.   if (0==argc) exit(1); // quit if started from the Workbench
  104. #endif
  105.  
  106.   program_name = argv[0];
  107.  
  108.   /* Default debugging level.  */
  109.   pfs_debug = 0;
  110.  
  111. #ifdef    CUTCP
  112.   if (getenv ("CONFIGTEL"))
  113.     if (Shostfile (getenv ("CONFIGTEL")) < 0)
  114.       {
  115.     fprintf (stderr, "Error, couldn't open configtel file %s\n",
  116.          getenv ("CONFIGTEL"));
  117.     exit (1);
  118.       }
  119. #endif
  120.  
  121.   if ((p = getenv ("ARCHIE_HOST")) != (char *) NULL)
  122.     host = p;
  123.  
  124. #ifdef CUTCP
  125.   while ((optc = getopt (argc, argv, "D:LHN:O:ceh:alm:o:rstvV")) != EOF)
  126. #else
  127.   while ((optc = getopt (argc, argv, "D:LN:O:ceh:alm:o:rstvV")) != EOF)
  128. #endif
  129.     {
  130.       switch (optc)
  131.     {
  132.     case 'D':
  133.       pfs_debug = atoi (optarg);
  134.       break;
  135.  
  136.     case 'L':
  137.       printf ("Known archie servers:\n");
  138.       for (tmp = 0; tmp < NARCHIES; tmp++)
  139.         printf ("\t%s\n", archies[tmp]);
  140.       printf (" * %s is the default Archie server.\n", ARCHIE_HOST);
  141.       printf (" * For the most up-to-date list, write to an Archie server and give it\n   the command `servers'.\n");
  142.       exitflag = 1;
  143.       break;
  144.  
  145. #ifdef CUTCP
  146.     case 'H':
  147.       if (Shostfile (optarg) < 0)
  148.         {
  149.           fprintf (stderr,
  150.                "%s: couldn't open configtel file %s\n",
  151.                program_name, optarg);
  152.           exit (1);
  153.         }
  154.       break;
  155. #endif
  156.  
  157.     case 'N':
  158.       rdgram_priority = atoi (optarg);
  159.       if (rdgram_priority > RDGRAM_MAX_SPRI)
  160.         rdgram_priority = RDGRAM_MAX_PRI;
  161.       else if (rdgram_priority < RDGRAM_MIN_PRI)
  162.         rdgram_priority = RDGRAM_MIN_PRI;
  163.       break;
  164.  
  165.     case 'c': /* Substring (case-sensitive).  */
  166.       query = SUBSTRING_CASE;
  167.       break;
  168.  
  169.     case 'e': /* Exact match.  */
  170.       query = EXACT;
  171.       break;
  172.  
  173.     case 'h': /* Archie host.  */
  174.       host = optarg;
  175. #ifdef CUTCP
  176.       hostset = 1;
  177. #endif
  178.       break;
  179.  
  180.     case 'a': /* List matches as Alex filenames.  */
  181.       alex = 1;
  182.       break;
  183.  
  184.     case 'l': /* List one match per line.  */
  185.       listflag = 1;
  186.       break;
  187.  
  188.     case 'm': /* Maximum number of hits for the query.  */
  189.       max_hits = atoi (optarg);
  190.       if (max_hits < 1)
  191.         {
  192.           fprintf (stderr,
  193.                "%s: option `-m' requires a max hits value >= 1\n",
  194.                program_name);
  195.           exit (ERROR_EXIT);
  196.         }
  197.       break;
  198.  
  199.     case 'o': /* output file */
  200.       if (outfile)
  201.         {
  202.           fprintf (stderr, "%s: multiple output files specified\n",
  203.                program_name);
  204.           exit (ERROR_EXIT);
  205.         }
  206.       outfile = optarg;
  207.       break;
  208.  
  209.     case 'O': /* Specify the offset.  */
  210.       offset = atoi (optarg);
  211.       break;
  212.  
  213.     case 'r': /* Regexp search.  */
  214.       query = REGEXP;
  215.       break;
  216.  
  217.     case 's': /* Substring (case insensitive).  */
  218.       query = SUBSTRING;
  219.       break;
  220.  
  221.     case 't': /* Sort inverted by date.  */
  222.       sortflag = 1;
  223.       break;
  224.  
  225.     case 'v': /* Display version.  */
  226.       fprintf (stderr,
  227. #ifdef AMIGA
  228.         VSTRING "\n"
  229. #endif /* AMIGA */
  230.            "Client version %s based upon Prospero version %s\n",
  231.            CLIENT_VERSION, PFS_RELEASE);
  232.       exitflag = 1;
  233.       break;
  234.  
  235.     case 'V': /* Verbose when search is happening.  */
  236.       verbose = 1;
  237.       break;
  238.  
  239.     default:
  240.       usage ();
  241.     }
  242.     }
  243.  
  244.   if (exitflag)
  245.     exit (0);
  246.   else if (optind == argc)
  247.     usage ();
  248.   else if (alex && listflag)
  249.     {
  250.       fprintf (stderr, "%s: only one of `-a' or `-l' may be used\n",
  251.            program_name);
  252.       exit (ERROR_EXIT);
  253.     }
  254.  
  255.   if (outfile)
  256.     {
  257.       archie_out = fopen (outfile, "w+");
  258.       if (archie_out == (FILE *) NULL)
  259.     {
  260.       fprintf (stderr, "%s: cannot open %s\n", program_name, outfile);
  261.       exit (ERROR_EXIT);
  262.     }
  263.     }
  264.   else
  265.     archie_out = stdout;
  266.  
  267. #ifdef    CUTCP
  268.   if (tmp = Snetinit ())
  269.     {
  270.       fprintf (stderr, " %d from SNetinit (bad or missing config.tel ?)\n",
  271.            tmp);
  272.  
  273.       /* If there was a rarp lookup failure, shut the network card down.  */
  274.       if (tmp == -2)
  275.     netshut ();
  276.       exit (ERROR_EXIT);
  277.     }
  278.  
  279.   if (!hostset)
  280.     {
  281.       /* Look in config.tel if they didn't give us a host with `-h'.  The
  282.          entries appear as "name=archie".  */
  283.       struct machinfo *mp = Shostlook ("archie");
  284.  
  285.       if (mp)
  286.     host = mp->hname ? mp->hname : mp->sname;
  287.     }
  288. #endif
  289.  
  290.   for (; optind < argc; ++optind)
  291.     procquery (host, argv[optind], max_hits, offset, query);
  292.  
  293. #ifdef CUTCP
  294.   netshut ();
  295. #endif
  296.  
  297.   if (outfile)
  298.     fclose (archie_out);
  299.  
  300.   exit (0);
  301. }
  302.  
  303. #ifdef CUTCP
  304. # define HFLAG    "] [H config.tel]"
  305. #else
  306. # define HFLAG    "]"
  307. #endif
  308.  
  309. void
  310. usage (void)
  311. {
  312.   fprintf (stderr, "\
  313. Usage: %s [-acelorstvLV] [-m hits%s [-N level] string\n", program_name, HFLAG);
  314.   fprintf (stderr, "          -a : list matches as Alex filenames\n");
  315.   fprintf (stderr, "          -c : case sensitive substring search\n");
  316.   fprintf (stderr, "          -e : exact string match (default)\n");
  317.   fprintf (stderr, "          -r : regular expression search\n");
  318.   fprintf (stderr, "          -s : case insensitive substring search\n");
  319.   fprintf (stderr, "          -l : list one match per line\n");
  320.   fprintf (stderr, "          -t : sort inverted by date\n");
  321.   fprintf (stderr, "     -m hits : specifies maximum number of hits to return (default %d)\n", max_hits);
  322.   fprintf (stderr, " -o filename : specifies file to store results in\n");
  323.   fprintf (stderr, "     -h host : specifies server host\n");
  324.   fprintf (stderr, "          -L : list known servers and current default\n");
  325.   fprintf (stderr, "    -N level : specifies query niceness level (0-35765)\n");
  326. #ifdef CUTCP
  327.   fprintf (stderr, "-H config.tel: specify location of config.tel file\n");
  328. #endif
  329.  
  330.   exit (ERROR_EXIT);
  331. }
  332.  
  333.